package com.bsierp.service;

import java.io.Serializable;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

@SuppressWarnings("serial")
public class DownloadService implements Serializable {

	/**
	 * @param workbook
	 * @param fileName
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	public static void exportExcelFile(HSSFWorkbook workbook, String fileName,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		response.reset();
		fileName = "attachment;filename=\"" + fileName + "\";";
		response.setHeader("Content-disposition", fileName);
		response.setContentType("application/vnd.ms-excel");
		// export as the excel
		workbook.write(response.getOutputStream());
		response.getOutputStream().flush();
		response.getOutputStream().close();
	}

	/**
	 * @param row
	 * @param cellCount
	 * @param value
	 * @param titleStyle
	 * @return
	 */
	@SuppressWarnings("deprecation")
	protected static HSSFCell createCell(HSSFRow row, short cellCount,
			String value, HSSFCellStyle titleStyle) {
		HSSFCell cell = row.createCell(cellCount);
		//cell.setEncoding((short) 1);
		if (titleStyle != null)
			cell.setCellStyle(titleStyle);
		cell.setCellValue(value);
		return cell;
	}

	/**
	 * @return
	 * @throws Exception
	 */
	public static HSSFWorkbook createWorkbook() throws Exception {
		HSSFWorkbook wb = new HSSFWorkbook();
		@SuppressWarnings("unused")
		HSSFSheet s = wb.createSheet();
		return wb;
	}

	/**
	 * @param sheetCount
	 * @return
	 * @throws Exception
	 */
	public static HSSFWorkbook createWorkbook(int sheetCount) throws Exception {
		HSSFWorkbook wb = new HSSFWorkbook();
		for (int i = 0; i < sheetCount; i++) {
			@SuppressWarnings("unused")
			HSSFSheet si = wb.createSheet();
		}

		return wb;
	}
}
